﻿--[[
   @brief    suggest predownload tips control
   @author   leozzyzheng
   @version  1.0
   @date     2015-03-26
--]]

suggest_predownload_control = {
    
}

function suggest_predownload_control:init()
    ierd_tgp.tpf_ui.Require("suggest_predownload\\suggest_predownload_view.lua");
    ierd_tgp.tpf_ui.Require("suggest_predownload\\suggest_predownload_model.lua");
end;

function suggest_predownload_control.on_login_success()
    suggest_predownload_model:query_cfg();
end;

function suggest_predownload_control.suggest_predownload(event_id, json)
    -- 避开这个更新提示框弹出的时候
    local pDlg = ui_utility.get_ctrl_ptr_by_cid(CID_AUTO_UPDATE_DLG);
    if pDlg ~= nil and pDlg:IsVisible() == TRUE then
        base_utility.log("[suggest_predownload_control]CID_AUTO_UPDATE_DLG exist");
        return;
    end;

    local json_table = base_utility.json_to_table(json);
    if json_table ~= nil and suggest_predownload_model:check_valied() then
        if json_table.game_id ~= nil and json_table.game_id ~= "" then
            local game_id = tonumber(json_table.game_id);
            -- 检查是否支持并且还未设置自动更新
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
                local game_state = ierd_tgp.game_library.GameState:new();
                game_lib:get_game_state(game_id, game_state);
                base_utility.log("[suggest_predownload_control]iSupport_update:" .. game_state.iSupport_update .. "iAuto_update" .. game_state.iAuto_update);
                if game_state.iSupport_update and   -- 支持自动更新
                   game_state.iAuto_update == 0 then  -- 还未设置自动更新
                    local num = suggest_predownload_model:get_number(game_id);
                    if num > 0 then
                        base_utility.log("[suggest_predownload_control]number:" .. num);
                        suggest_predownload_view:open(game_id, num);
                    end;
                end;
                game_state:delete();
            end;
        end;
    end;
end;

function suggest_predownload_control.force_close()
    suggest_predownload_view.close_reason = suggest_predownload_view.CLOSE_REASON_AVOID_OTHERS;
    suggest_predownload_view.close();
end;

-- qos上报
function suggest_predownload_control:qos()
    local qos_param = ierd_tgp.tpf_ui.LuaQosData:new();
    qos_param.oc_type = ierd_tgp.tpf_ui.kInstant; --即时
    qos_param.qos_report_id = ierd_tgp.adapt_for_imports.qos.Qos_kind.kSuggestPredownload;
    qos_param.int_val[0] = suggest_predownload_view.close_reason;  -- 窗口关闭的情况，1.点击按钮关闭，2.系统消息关闭，3.避让关闭
    qos_param.int_val[1] = suggest_predownload_view.btn_clicked;  -- 上报用户点击的行为1.啥都没点 2.点了x 3.点了确认开启
    qos_param.int_count = 2;
    qos_param.str_count = 0;
    base_utility.lua_qos(qos_param);
    qos_param:delete();
end;

suggest_predownload_control:init();
